package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.entity.ShaneLikesAnswer;
import com.baizhi.shane.shane.vo.EssayUserRelVO;
import com.baizhi.shane.sys.manager.RedisManager;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.entity.ShaneLikesEssay;
import com.baizhi.shane.shane.service.ShaneLikesEssayService;
import com.baizhi.shane.shane.mapper.ShaneLikesEssayMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author wbz12
* @description 针对表【shane_likes_essay(啥呢文章点赞表)】的数据库操作Service实现
* @createDate 2023-07-25 22:33:37
*/
@Service
public class ShaneLikesEssayServiceImpl extends ServiceImpl<ShaneLikesEssayMapper, ShaneLikesEssay>
    implements ShaneLikesEssayService{

    @Resource
    private RedisManager redisManager;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 用户点赞文章，只需要answerId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 21:23 2023/10/24
     * @Param essayUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String insertOneEssayLikes(EssayUserRelVO essayUserRelVO) {
        Long userId = redisManager.getUserId();
        Long essayId = essayUserRelVO.getEssayId();
        ShaneLikesEssay shaneLikesEssay = new ShaneLikesEssay();
        shaneLikesEssay.setUserId(userId);
        shaneLikesEssay.setEssayId(essayId);
        boolean save = this.save(shaneLikesEssay);
        String succeed = commonUtils.boolToString(save, SysMapping.LIKES_SUCCESS, SysMapping.LIKES_FAIL);
        return succeed;
    }

    /**
     * 用户取消点赞文章，只需要answerId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 21:28 2023/10/24
     * @Param essayUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String deleteOneEssayLikes(EssayUserRelVO essayUserRelVO) {
        Long userId = redisManager.getUserId();
        Long essayId = essayUserRelVO.getEssayId();

        LambdaUpdateWrapper<ShaneLikesEssay> uwSle = new LambdaUpdateWrapper<>();
        uwSle.eq(ShaneLikesEssay::getUserId,userId);
        uwSle.eq(ShaneLikesEssay::getEssayId,essayId);
        uwSle.eq(ShaneLikesEssay::getDelFlag,0);
        uwSle.set(ShaneLikesEssay::getDelFlag, 1);
        uwSle.set(ShaneLikesEssay::getDelBy, userId);
        uwSle.set(ShaneLikesEssay::getDelTime, new Date());
        boolean update = this.update(uwSle);
        String succeed = commonUtils.boolToString(update, SysMapping.LIKES_CANCEL_SUCCESS, SysMapping.LIKES_CANCEL_FAIL);
        return succeed;
    }
}




